package com.ansion.basedb.database.sql;

import androidx.annotation.NonNull;

import com.ansion.basetool.preconditions.Preconditions;

/**
 * created by zhaoyuntao
 * on 10/11/2023
 */
public class SQLField {
    private String tableName;
    private final String name;
    private int versionCode;
    private boolean index;
    private boolean primaryKey;
    private boolean autoIncrement;
    private boolean nonNull;
    private boolean unique;
    @ColumnType
    private int type = ColumnType.TYPE_TEXT;

    public SQLField(@NonNull String name) {
        this.name = name;
    }

    SQLField setTableName(String tableName) {
        this.tableName = tableName;
        return this;
    }

   public SQLField setVersionCode(int versionCode) {
        this.versionCode = versionCode;
        return this;
    }

    public SQLField index(boolean index) {
        this.index = index;
        return this;
    }

    public SQLField setType(@ColumnType int type) {
        this.type = type;
        return this;
    }

    public SQLField setPrimaryKey(boolean primaryKey) {
        return setPrimaryKey(primaryKey, false);
    }

    public SQLField setPrimaryKey(boolean primaryKey, boolean autoIncrement) {
        this.primaryKey = primaryKey;
        this.autoIncrement = autoIncrement;
        return this;
    }

    public SQLField setUnique(boolean unique) {
        Preconditions.checkArgument(!primaryKey, "Try to set unique on primary key.");
        this.unique = unique;
        return this;
    }

    public SQLField setNonNull(boolean nonNull) {
        Preconditions.checkArgument(!primaryKey, "Try to set canBeNull on primary key.");
        this.nonNull = nonNull;
        return this;
    }

    @NonNull
    public String getTableName() {
        return tableName;
    }

    @NonNull
    public String getName() {
        return name;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public boolean isIndex() {
        return index;
    }

    @ColumnType
    public int getType() {
        return type;
    }

    public boolean isPrimaryKey() {
        return primaryKey;
    }

    public boolean isAutoIncrement() {
        return autoIncrement;
    }

    public boolean isUnique() {
        return unique;
    }

    public boolean isNonNull() {
        return nonNull;
    }
}
